package com.quanyan.orderpay.task.tracker;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.quanyan.orderpay.scheduler.OrderPayTask;
import com.quanyan.orderpay.service.OrderPayService;
import com.quanyan.orderpay.service.OrderProcessService;
import com.quanyan.orderpay.service.check.account.SaveAccountWaterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author wmb
 */
public class OrderPayJobRunner implements JobRunner {

    private static final Logger logger = LoggerFactory.getLogger(OrderPayJobRunner.class);
    @Autowired
    private OrderProcessService orderProcessService;

    @Autowired
    private SaveAccountWaterService saveAccountWaterService;

    @Override
    public Result run(JobContext jobContext) throws Throwable {
        logger.debug("------TaskJobRunner执行了---------" + jobContext);
        Job job = jobContext.getJob();//对于需要执行多个任务的业务场景，可以使用这种方式
        String jobType = job.getParam("type");
        //订单关闭
        if (OrderPayTask.ORDER_CLOSE.equals(jobType)) {
            try {
                orderProcessService.closeTimeOutOrder();
                return new Result(Action.EXECUTE_SUCCESS, "--"+OrderPayTask.ORDER_CLOSE+"---task-execute-success----");
            } catch (Exception e) {
                logger.info("Run job failed!", e);
                return new Result(Action.EXECUTE_LATER, "--"+OrderPayTask.ORDER_CLOSE+"--task-execute-fail--->"+e.getMessage());
            }
        } else if(OrderPayTask.ORDER_MQ.equals(jobType)){
            //订单消息重发
            try {
                this.orderProcessService.orderMqResend();
                return new Result(Action.EXECUTE_SUCCESS, "--"+OrderPayTask.ORDER_MQ+"---task-execute-success----");
            } catch (Exception e) {
                logger.info("Run job failed!", e);
                return new Result(Action.EXECUTE_LATER, "--"+OrderPayTask.ORDER_MQ+"--task-execute-fail--->"+e.getMessage());
            }
        } else if(OrderPayTask.CALL_BACK_SCAN_PAY.equals(jobType)){
            //扫码枪订单消息重发
            try {
                this.orderProcessService.callBackSacnPayResend();
                return new Result(Action.EXECUTE_SUCCESS, "--"+OrderPayTask.CALL_BACK_SCAN_PAY+"---task-execute-success----");
            } catch (Exception e) {
                logger.info("Run job failed!", e);
                return new Result(Action.EXECUTE_LATER, "--"+OrderPayTask.CALL_BACK_SCAN_PAY+"--task-execute-fail--->"+e.getMessage());
            }
        }else if(OrderPayTask.REFUND_STATUS_QUERY.equals(jobType)){
            //微信退款查询
            try {
                orderProcessService.refundComplateProcess();
                return new Result(Action.EXECUTE_SUCCESS, "--"+OrderPayTask.REFUND_STATUS_QUERY+"---task-execute-success----");
            } catch (Exception e) {
                logger.info("Run job failed!", e);
                return new Result(Action.EXECUTE_LATER, "--"+OrderPayTask.REFUND_STATUS_QUERY+"--task-execute-fail--->"+e.getMessage());
            }
        }else if(OrderPayTask.WITHDRAW_RESET.equals(jobType)){
            //提现刷新状态
            try {
                orderProcessService.withdrawReset();
                return new Result(Action.EXECUTE_SUCCESS, "--"+OrderPayTask.WITHDRAW_RESET+"---task-execute-success----");
            } catch (Exception e) {
                logger.info("Run job failed!", e);
                return new Result(Action.EXECUTE_LATER, "--"+OrderPayTask.WITHDRAW_RESET+"--task-execute-fail--->"+e.getMessage());
            }
        } else if (OrderPayTask.WEICHAT_ACCESS_TOKEN.equals(jobType)) {
            //微信定时刷新AccessToken
            try {
                orderProcessService.refreshAccessToken();
                return new Result(Action.EXECUTE_SUCCESS, "--"+OrderPayTask.WEICHAT_ACCESS_TOKEN+"---task-execute-success----");
            } catch (Exception e) {
                logger.info("Run job failed!", e);
                return new Result(Action.EXECUTE_LATER, "--"+OrderPayTask.WEICHAT_ACCESS_TOKEN+"--task-execute-fail--->"+e.getMessage());
            }
        } else if (OrderPayTask.ORDER_QUERY.equals(jobType)) {
            //扫描枪订单查询
            try {
                orderProcessService.orderQuery();
                return new Result(Action.EXECUTE_SUCCESS, "--"+OrderPayTask.WEICHAT_ACCESS_TOKEN+"---task-execute-success----");
            } catch (Exception e) {
                logger.info("Run job failed!", e);
                return new Result(Action.EXECUTE_LATER, "--"+OrderPayTask.WEICHAT_ACCESS_TOKEN+"--task-execute-fail--->"+e.getMessage());
            }
        } else if (OrderPayTask.SCAN_PAY.equals(jobType)) {
            //扫描枪订单查询
            try {
                orderProcessService.scanPay();
                return new Result(Action.EXECUTE_SUCCESS, "--"+OrderPayTask.WEICHAT_ACCESS_TOKEN+"---task-execute-success----");
            } catch (Exception e) {
                logger.info("Run job failed!", e);
                return new Result(Action.EXECUTE_LATER, "--"+OrderPayTask.WEICHAT_ACCESS_TOKEN+"--task-execute-fail--->"+e.getMessage());
            }
        } else if (OrderPayTask.ACCOUNT_WATER_DOWNLOAD.equals(jobType)) {
            //下载前一天微信对账单
            saveAccountWaterService.saveYesterdayWechatAccountWater();
            //下载前一天支付宝对账单
            saveAccountWaterService.saveYesterdayAlipayAccountWater();
            //TODO: 下载前一天银联对账单，银联商务未完成对账单上传
            //saveAccountWaterService.saveYesterdayChinaumsAccountWater();
        } {
            return new Result(Action.EXECUTE_SUCCESS, "-------no execute task---------------");
        }
    }

}
